#include "Testing.h"
#include <iostream>
using namespace std;

#include "Distributed/DistributedPackets.h"
#include "Solution/Solution.h"
#include "Solution/SolutionFactory.h"
#include "Solution/BitEncoded/BitEncodedSolution.h"
#include "Solution/PermutationEncoded/PermutationEncodedSolution.h"

void poisonPacket(Packet &p) {
    p.signal = SUBMISSION_RECIEVED;
    p.jobType = CROSSOVER;
    p.solPtr1 = NULL;
    p.solPtr2 = NULL;
    p.fitness = 99999;
}


void testSerializationStuff() {
    char buffer[4096];
    
    BitEncodedSolution* bes1 = new BitEncodedSolution();
    BitEncodedSolution* bes2;
     
    bes1->addBit(true);
    bes1->addBit(false);
    bes1->addBit(true);
    bes1->addBit(false);
    bes1->addBit(true);
    bes1->addBit(false);
    bes1->addBit(true); 
    bes1->addBit(false);
    bes1->addBit(true);
    bes1->addBit(false);
    
    int solLength = SolutionFactory::serializeSolution(buffer, 4096, bes1);
    bes2 = (BitEncodedSolution*) SolutionFactory::deserializeSolution(buffer, solLength);
    
    bes1->outputSolution(cout) << endl;
    bes2->outputSolution(cout) << endl;
    
    delete bes1;
    delete bes2;
    
    PermutationEncodedSolution* pes1 = new PermutationEncodedSolution();
    PermutationEncodedSolution* pes2;
    
    pes1->addElement(5);
    pes1->addElement(4);
    pes1->addElement(3);
    pes1->addElement(2);
    pes1->addElement(1);
    
    solLength = SolutionFactory::serializeSolution(buffer, 4096, pes1);
    pes2 = (PermutationEncodedSolution*) SolutionFactory::deserializeSolution(buffer, solLength);
    
    pes1->outputSolution(cout) << endl;
    pes2->outputSolution(cout) << endl;
    
}

void testPacketStuff() {
    char buffer[4096];
    Packet p1;
    Packet p2;
    p1.signal = JOB_SUBMISSION;
    p1.jobType = CROSSOVER;
    p1.solPtr1 = new BitEncodedSolution();
    p1.solPtr2 = new BitEncodedSolution();
    p1.fitness = 12345;
    poisonPacket(p2);
    int packetLength = DistributedPackets::packetToBytes(buffer, 4096, p1);
    DistributedPackets::bytesToPacket(buffer, packetLength, p2);
    cout << "test!" << endl;
}

void Testing::doTest() {
    testSerializationStuff();
}

